# Docker Desktop 修复脚本
# 使用方法：以管理员身份运行 PowerShell，然后执行 .\fix-docker.ps1

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  Docker Desktop 诊断和修复工具" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# 检查是否以管理员身份运行
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
    Write-Host "❌ 请以管理员身份运行此脚本" -ForegroundColor Red
    Write-Host ""
    Write-Host "右键点击 PowerShell → 选择 '以管理员身份运行'" -ForegroundColor Yellow
    exit 1
}

Write-Host "✅ 已以管理员身份运行" -ForegroundColor Green
Write-Host ""

# 步骤1：停止 Docker 相关进程
Write-Host "[1/6] 停止 Docker 相关进程..." -ForegroundColor Yellow
$dockerProcesses = Get-Process "*docker*" -ErrorAction SilentlyContinue
if ($dockerProcesses) {
    Write-Host "  发现以下 Docker 进程：" -ForegroundColor Cyan
    $dockerProcesses | ForEach-Object { Write-Host "    - $($_.ProcessName) (PID: $($_.Id))" }
    
    Write-Host "  正在停止进程..." -ForegroundColor Cyan
    Get-Process "*docker*" -ErrorAction SilentlyContinue | Stop-Process -Force
    Start-Sleep -Seconds 3
    Write-Host "  ✅ Docker 进程已停止" -ForegroundColor Green
} else {
    Write-Host "  ⏭️  未发现运行中的 Docker 进程" -ForegroundColor Gray
}
Write-Host ""

# 步骤2：检查 WSL 2 状态
Write-Host "[2/6] 检查 WSL 2 状态..." -ForegroundColor Yellow
try {
    $wslVersion = wsl --version 2>&1
    Write-Host "  ✅ WSL 已安装" -ForegroundColor Green
    
    Write-Host "  正在关闭 WSL..." -ForegroundColor Cyan
    wsl --shutdown
    Start-Sleep -Seconds 5
    Write-Host "  ✅ WSL 已关闭" -ForegroundColor Green
} catch {
    Write-Host "  ⚠️  WSL 未安装或未配置" -ForegroundColor Yellow
    Write-Host "  正在安装 WSL..." -ForegroundColor Cyan
    wsl --install
    Write-Host "  ✅ WSL 安装完成，需要重启电脑" -ForegroundColor Green
}
Write-Host ""

# 步骤3：检查 Hyper-V 状态
Write-Host "[3/6] 检查 Hyper-V 状态..." -ForegroundColor Yellow
$hypervService = Get-Service vmcompute -ErrorAction SilentlyContinue
if ($hypervService) {
    if ($hypervService.Status -eq 'Running') {
        Write-Host "  ✅ Hyper-V 服务正在运行" -ForegroundColor Green
    } else {
        Write-Host "  ⚠️  Hyper-V 服务未运行，正在启动..." -ForegroundColor Yellow
        Start-Service vmcompute
        Write-Host "  ✅ Hyper-V 服务已启动" -ForegroundColor Green
    }
} else {
    Write-Host "  ⚠️  未找到 Hyper-V 服务" -ForegroundColor Yellow
    Write-Host "  提示：可能需要在 Windows 功能中启用 Hyper-V" -ForegroundColor Cyan
}
Write-Host ""

# 步骤4：清理 Docker 缓存（可选）
Write-Host "[4/6] 是否清理 Docker 缓存数据？(会删除所有容器和镜像)" -ForegroundColor Yellow
Write-Host "  输入 'y' 清理，输入 'n' 跳过: " -NoNewline
$cleanCache = Read-Host

if ($cleanCache -eq 'y' -or $cleanCache -eq 'Y') {
    Write-Host "  正在清理 Docker 数据..." -ForegroundColor Cyan
    
    # 删除 Docker Desktop 数据
    $paths = @(
        "$env:APPDATA\Docker",
        "$env:LOCALAPPDATA\Docker"
    )
    
    foreach ($path in $paths) {
        if (Test-Path $path) {
            Remove-Item -Path $path -Recurse -Force -ErrorAction SilentlyContinue
            Write-Host "  ✅ 已删除: $path" -ForegroundColor Green
        }
    }
} else {
    Write-Host "  ⏭️  跳过清理" -ForegroundColor Gray
}
Write-Host ""

# 步骤5：更新 WSL 2
Write-Host "[5/6] 更新 WSL 2..." -ForegroundColor Yellow
try {
    wsl --update
    Write-Host "  ✅ WSL 更新完成" -ForegroundColor Green
} catch {
    Write-Host "  ⚠️  WSL 更新失败" -ForegroundColor Yellow
}
Write-Host ""

# 步骤6：启动 Docker Desktop
Write-Host "[6/6] 启动 Docker Desktop..." -ForegroundColor Yellow
$dockerPath = "C:\Program Files\Docker\Docker\Docker Desktop.exe"

if (Test-Path $dockerPath) {
    Start-Process $dockerPath
    Write-Host "  ✅ Docker Desktop 已启动" -ForegroundColor Green
    Write-Host ""
    Write-Host "  ⏳ 请等待 2-3 分钟，让 Docker Engine 完全启动..." -ForegroundColor Cyan
    Write-Host "  查看任务栏的 Docker 图标，等待变为绿色" -ForegroundColor Cyan
} else {
    Write-Host "  ❌ 未找到 Docker Desktop" -ForegroundColor Red
    Write-Host "  请确认 Docker Desktop 已安装" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "  修复脚本执行完成！" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""

Write-Host "📝 后续步骤：" -ForegroundColor Yellow
Write-Host "1. 等待 Docker Desktop 启动完成（任务栏图标变绿）" 
Write-Host "2. 打开新的 PowerShell 窗口"
Write-Host "3. 运行: docker ps"
Write-Host "4. 如果成功，返回项目继续操作"
Write-Host ""
Write-Host "⚠️  如果还是无法启动，请尝试：" -ForegroundColor Yellow
Write-Host "   - 重启电脑"
Write-Host "   - 卸载并重新安装 Docker Desktop"
Write-Host "   - 检查防火墙或杀毒软件是否阻止了 Docker"
Write-Host ""

